package com.dwh.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 
 * 日志记录切面               .<br>
 *
 * @author dongwenhua
 * @version 2017年12月27日
 */
@Aspect
@Component
public class LogAspect {
	private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

	/*@Before(value = "execution(public * com.dwh.controller.*.*(..))")
	public void before() {
		System.out.println("-----------------");
	}
	
	@After(value = "execution(public * com.dwh.controller.*.*(..))")
	public void after() {
		System.out.println("after");
	}*/
	
	@Pointcut(value = "execution(public * com.dwh.controller.*.*(..))")
	public void log() {
		
	}
	
	@Before("log()")
	public void before(JoinPoint joinPoint) {
		//记录请求的url、ip、方法名、参数
		ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		log.info("ip={}", request.getRemoteAddr());
		log.info("url={}", request.getRequestURL());
		log.info("way={}", request.getMethod());
		joinPoint.getSignature().getDeclaringType();
		log.info("method={}", joinPoint.getSignature().getDeclaringType() + "." + joinPoint.getSignature().getName());
		log.info("param={}", joinPoint.getArgs());
	}
	
	@AfterReturning(returning = "obj", pointcut = "log()")
	public void After(Object obj) {
		log.info("response={}", obj);
	}
	
	@After("log()")
	public void after() {
		log.info("after");
	}

}
